package com.xqb.common.utils;

import com.alibaba.fastjson.JSON;
import com.xqb.modules.domain.dto.AiPaperCutDto;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author: JinChen Zhu
 * @CreateTime: 2024-10-30  23:45
 */
@Component
@Slf4j
public class AIUtils {
    //切题接口
    @Value("${ai.host}")
    private String host;

    @Value("${ai.appcode}")
    private String appCode;


    public void paperCut(AiPaperCutDto pcd) {
        String path = "/educationservice/papercut";
        String method = "POST";
        // 假设这些值从配置文件或环境变量中获取

        Map<String, String> headers = new HashMap<>();
        headers.put("Authorization", "APPCODE " + appCode);
        headers.put("Content-Type", "application/json; charset=UTF-8");

        Map<String, String> querys = new HashMap<>();
        // 构建合法的 JSON 请求体，移除注释并使用变量替换硬编码值
        String body = JSON.toJSONString(pcd);


        try {
            HttpResponse response = HttpUtils.doPost(host, path, method, headers, querys, body);
            // 记录响应信息
            log.info("Response Status: " + response.getStatusLine().getStatusCode());

            // 处理响应体
            String responseBody = EntityUtils.toString(response.getEntity());

            log.info("Response Body: " + responseBody);
        } catch (Exception e) {
            // 记录异常信息
            log.error("Failed to cut subjects", e);
            // 抛出运行时异常或自定义异常，通知调用者处理
            throw new RuntimeException("Failed to cut subjects", e);
        }
    }







}
